/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-5-17 上午10:31
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.recycleviewdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

import com.example.dqhl.recycleviewdemo.R;
import com.example.dqhl.recycleviewdemo.adapter.StraggedGridAdapter;
import com.example.dqhl.recycleviewdemo.modle.StraggedModle;
import com.example.dqhl.recycleviewdemo.util.DividerStraggeDecoration;

import java.util.ArrayList;
import java.util.List;
/**
*description:瀑布流
*autour:guanluocang
*date:17/5/24 16:09
*/
public class StraggedGridActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private StraggedGridAdapter straggedGridAdapter;
    private List<StraggedModle> straggedModleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stragged_grid);
        initData();
        /**
         *
         *  实现左边一张大图 右边两张小图的话  只要把坐标的大图 高度跳高即可
         *
         * */
        (mRecyclerView) = (RecyclerView) findViewById(R.id.recyceView);
        //设置布局
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //setAdapter
        mRecyclerView.setAdapter(straggedGridAdapter = new StraggedGridAdapter(this, straggedModleList));
        mRecyclerView.setLayoutAnimation(getAnimationController());
        //设置分割线 左右距离
        mRecyclerView.addItemDecoration(new DividerStraggeDecoration(16));
        //点击事件分为点击和长按事件
        straggedGridAdapter.setOnItemClickListener(new StraggedGridAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(StraggedGridActivity.this, position + " click",
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(StraggedGridActivity.this, position + " long click",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        straggedModleList = new ArrayList<>();
        StraggedModle p1 = new StraggedModle(R.mipmap.img_three, "我是照片1");
        straggedModleList.add(p1);
        StraggedModle p2 = new StraggedModle(R.mipmap.img_one, "我是照片2");
        straggedModleList.add(p2);
        StraggedModle p3 = new StraggedModle(R.mipmap.bg_js, "我是照片3");
        straggedModleList.add(p3);
        StraggedModle p4 = new StraggedModle(R.mipmap.bg_other, "我是照片4");
        straggedModleList.add(p4);
        StraggedModle p5 = new StraggedModle(R.mipmap.img_three, "我是照片5");
        straggedModleList.add(p5);
        StraggedModle p6 = new StraggedModle(R.mipmap.bg_ios, "我是照片6");
        straggedModleList.add(p6);
        StraggedModle p7 = new StraggedModle(R.mipmap.bg_js, "我是照片7");
        straggedModleList.add(p7);
        StraggedModle p8 = new StraggedModle(R.mipmap.img_fout, "我是照片8");
        straggedModleList.add(p8);
        StraggedModle p9 = new StraggedModle(R.mipmap.bg_android, "我是照片9");
        straggedModleList.add(p9);
        StraggedModle p10 = new StraggedModle(R.mipmap.img_five, "我是照片10");
        straggedModleList.add(p10);
        StraggedModle p11 = new StraggedModle(R.mipmap.bg_js, "我是照片11");
        straggedModleList.add(p11);
    }

    /**
     * Layout动画
     *
     * @return
     */
    protected LayoutAnimationController getAnimationController() {
        int duration = 300;
        AnimationSet set = new AnimationSet(true);

        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(duration);
        set.addAnimation(animation);

        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        animation.setDuration(duration);
        set.addAnimation(animation);

        LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        return controller;
    }


}
